<?php
/**
 * CakeCMS - 焦点图管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
App::import ( 'Vendor', 'Uploader' );
class FocusController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->Uploader = new Uploader ();
		$this->Uploader->setup ( array (
				'tempDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'tmp' . DS,
				'baseDir' => CAKE_CORE_INCLUDE_PATH . DS . 'www' . DS . 'webroot' . DS,
				'uploadDir' => 'files' . DS . 'focus' . DS . date ( "Ymd", time () ) 
		) );
		
		$this->set ( 'title_for_layout', 'CakeCMS焦点图管理' );
	}
	public function index() {
		$this->set ( 'title_for_layout', 'CakeCMS焦点图管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'focus.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Focus" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$cate_id = isset ( $this->params ['named'] ['cate_id'] ) ? $this->params ['named'] ['cate_id'] : '';
		$keyword = isset ( $this->params ['named'] ['keyword'] ) ? $this->params ['named'] ['keyword'] : '';
		
		// 整理搜索条件
		$conditions = array ();
		
		if (! empty ( $cate_id )) {
			$conditions ['cate_id'] = $cate_id;
		}
		if (! empty ( $keyword )) {
			$conditions ['title LIKE'] = "%$keyword%";
		}
		
		$list = $this->Focus->GetFocusByList ( $conditions, $limit );
		if ($list) {
			$this->loadModel ( "FocusCate" );
			
			foreach ( $list ['result'] as &$val ) {
				$CateInfo = $this->FocusCate->GetFocusCateByID ( $val ['Focus'] ['cate_id'] );
				$val ['Focus'] ['cate_desc'] = $CateInfo ['FocusCate'] ['name'];
				
				// 缩略图
				$images = explode ( '.', $val ['Focus'] ['img'] );
				$val ['Focus'] ['img_small'] = $images [0] . '_' . $CateInfo ['FocusCate'] ['width'] . '.' . $images [1];
				$val ['Focus'] ['img_small'] = str_replace ( "\\", "/", $val ['Focus'] ['img_small'] );
			}
		}
		
		$this->set ( 'cate_id', $cate_id );
		$this->set ( 'keyword', $keyword );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
		
		$this->loadModel ( "FocusCate" );
		$conditions = array ();
		$result = $this->FocusCate->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'id ASC' 
				) 
		) );
		$this->set ( 'Catelist', $result );
	}
	
	/**
	 * 添加焦点图
	 */
	public function add() {
		if ($this->request->isPost ()) {
			$data ['Focus'] ['cate_id'] = $this->data ['cate_id'];
			if (empty ( $data ['Focus'] ['cate_id'] )) {
				$this->flash ( '所属分类不能为空!', '/Focus/add/', 1250 );
				return false;
			}
			
			$this->loadModel ( "FocusCate" );
			$CateInfo = $this->FocusCate->GetFocusCateByID ( $this->data ['cate_id'] );
			if (! $CateInfo) {
				$this->flash ( '所属分类错误!', '/Focus/add/', 1250 );
				return false;
			}
			
			$data ['Focus'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Focus'] ['title'] )) {
				$this->flash ( '标题不能为空!', '/Focus/add/', 1250 );
				return false;
			}
			$data ['Focus'] ['url'] = $this->data ['url'];
			if (empty ( $data ['Focus'] ['url'] )) {
				$this->flash ( '地址不能为空!', '/Focus/add/', 1250 );
				return false;
			}
			
			$data ['Focus'] ['order'] = $this->data ['order'];
			if (empty ( $data ['Focus'] ['order'] )) {
				$data ['Focus'] ['order'] = 0;
			}
			$data ['Focus'] ['content'] = $this->data ['content'];
			if (empty ( $data ['Focus'] ['content'] )) {
				$this->flash ( '摘要不能为空!', '/Focus/add/', 1250 );
				return false;
			}
			$data ['Focus'] ['status'] = $this->data ['status'];
			if (! isset ( $data ['Focus'] ['status'] )) {
				$this->flash ( '请选择审核状态!', '/Focus/add/', 1250 );
				return false;
			}
			
			if (empty ( $_FILES ['img'] ['name'] ) || ! is_array ( $_FILES ['img'] )) {
				$this->flash ( '图片不能为空!', '/Focus/add/', 1250 );
				return false;
			} elseif ($_FILES ['img'] ['type'] != 'image/jpeg') {
				$this->flash ( '请正确选择图片类型!', '/Focus/add/', 1250 );
				return false;
			} else {
				// 上传图片
				$image = $this->Uploader->upload ( 'img', array (
						'name' => date ( "YmdHis", time () ),
						'overwrite' => true 
				) );
				if (! $image) {
					$this->flash ( '图片上传错误!', '/Focus/add/', 1250 );
					return false;
				} else {
					
					// 缩图
					$this->Uploader->resize ( array (
							'width' => $CateInfo ['FocusCate'] ['width'],
							'height' => $CateInfo ['FocusCate'] ['height'],
							'append' => '_' . $CateInfo ['FocusCate'] ['width'],
							'quality' => 100 
					) );
					
					$data ['Focus'] ['img'] = $image ['path'];
				}
			}
			
			$this->loadModel ( "Focus" );
			if ($this->Focus->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加焦点图!' );
				
				$this->flash ( '添加焦点图成功!', '/Focus/', 1250 );
				return false;
			} else {
				$this->flash ( '添加焦点图失败!', '/Focus/add/', 1250 );
				return false;
			}
		} else {
			$this->loadModel ( "FocusCate" );
			$conditions = array ();
			$result = $this->FocusCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			$this->set ( 'Catelist', $result );
		}
	}
	/**
	 * 编辑焦点图
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$data ['Focus'] ['cate_id'] = $this->data ['cate_id'];
			if (empty ( $data ['Focus'] ['cate_id'] )) {
				$this->flash ( '所属分类不能为空!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			$this->loadModel ( "FocusCate" );
			$CateInfo = $this->FocusCate->GetFocusCateByID ( $this->data ['cate_id'] );
			if (! $CateInfo) {
				$this->flash ( '所属分类错误!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			$data ['Focus'] ['title'] = $this->data ['title'];
			if (empty ( $data ['Focus'] ['title'] )) {
				$this->flash ( '标题不能为空!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Focus'] ['url'] = $this->data ['url'];
			if (empty ( $data ['Focus'] ['url'] )) {
				$this->flash ( '地址不能为空!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			$data ['Focus'] ['order'] = $this->data ['order'];
			if (empty ( $data ['Focus'] ['order'] )) {
				$data ['Focus'] ['order'] = 0;
			}
			$data ['Focus'] ['content'] = $this->data ['content'];
			if (empty ( $data ['Focus'] ['content'] )) {
				$this->flash ( '摘要不能为空!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			$data ['Focus'] ['status'] = $this->data ['status'];
			if (! isset ( $data ['Focus'] ['status'] )) {
				$this->flash ( '请选择审核状态!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
			
			if (! empty ( $_FILES ['img'] ['name'] )) {
				// 上传图片
				$image = $this->Uploader->upload ( 'img', array (
						'name' => date ( "YmdHis", time () ),
						'overwrite' => true 
				) );
				if (! $image) {
					$this->flash ( '图片上传错误!', "/Focus/edit/{$this->data ['id']}/", 1250 );
					return false;
				} else {
					
					// 缩图
					$this->Uploader->resize ( array (
							'width' => $CateInfo ['FocusCate'] ['width'],
							'height' => $CateInfo ['FocusCate'] ['height'],
							'append' => '_' . $CateInfo ['FocusCate'] ['width'],
							'quality' => 100 
					) );
					
					$data ['Focus'] ['img'] = $image ['path'];
				}
			}
			
			$data ['Focus'] ['id'] = $this->data ['id'];
			
			$this->loadModel ( "Focus" );
			if ($this->Focus->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加焦点图!' );
				
				$this->flash ( '编辑焦点图成功!', '/Focus/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑焦点图失败!', "/Focus/edit/{$this->data ['id']}/", 1250 );
				return false;
			}
		} else {
			$this->loadModel ( "FocusCate" );
			$conditions = array ();
			$result = $this->FocusCate->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'id ASC' 
					) 
			) );
			$this->set ( 'Catelist', $result );
			
			$id = $this->params ['id'];
			
			$this->loadModel ( "Focus" );
			$FocusInfo = $this->Focus->GetFocusByID ( $id );
			
			$CateInfo = $this->FocusCate->GetFocusCateByID ( $FocusInfo ['Focus'] ['cate_id'] );
			
			// 缩略图
			$images = explode ( '.', $FocusInfo ['Focus'] ['img'] );
			$FocusInfo ['Focus'] ['img_small'] = $images [0] . '_' . $CateInfo ['FocusCate'] ['width'] . '.' . $images [1];
			
			$this->set ( 'FocusInfo', $FocusInfo );
		}
	}
	
	/**
	 * 删除焦点图
	 */
	public function delete() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Focus" );
		if ($this->Focus->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除焦点图,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 焦点图分类
	 */
	public function cate() {
		$this->set ( 'title_for_layout', 'CakeCMS焦点图分类管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'focus.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "FocusCate" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$list = $this->FocusCate->GetFocusCateByList ( $conditions, $limit );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 添加分类
	 */
	public function addCate() {
		if ($this->request->isPost ()) {
			$data ['FocusCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['FocusCate'] ['name'] )) {
				$this->flash ( '分类名称不能为空!', '/Focus/addCate/', 1250 );
				return false;
			}
			$data ['FocusCate'] ['width'] = $this->data ['width'];
			if (empty ( $data ['FocusCate'] ['width'] )) {
				$this->flash ( '宽度不能为空!', '/Focus/addCate/', 1250 );
				return false;
			}
			$data ['FocusCate'] ['height'] = $this->data ['height'];
			if (empty ( $data ['FocusCate'] ['height'] )) {
				$this->flash ( '高度不能为空!', '/Focus/addCate/', 1250 );
				return false;
			}
			$this->loadModel ( "FocusCate" );
			if ($this->FocusCate->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加焦点图分类!' );
				
				$this->flash ( '添加分类成功!', '/Focus/cate/', 1250 );
				return false;
			} else {
				$this->flash ( '添加分类失败!', '/Focus/addCate/', 1250 );
				return false;
			}
		} else {
			// 加载js文件
			$this->set ( 'scriptList', array (
					'focus.js' 
			) );
		}
	}
	/**
	 * 编辑分类
	 */
	public function editCate() {
		if ($this->request->isPost ()) {
			$data ['FocusCate'] ['name'] = $this->data ['name'];
			if (empty ( $data ['FocusCate'] ['name'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '分类名称不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['FocusCate'] ['width'] = $this->data ['width'];
			if (empty ( $data ['FocusCate'] ['width'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '宽度不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['FocusCate'] ['height'] = $this->data ['height'];
			if (empty ( $data ['FocusCate'] ['height'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '高度不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$data ['FocusCate'] ['id'] = $this->data ['id'];
			if (empty ( $data ['FocusCate'] ['id'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '参数错误!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$this->loadModel ( "FocusCate" );
			if ($this->FocusCate->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '焦点图分类,ID:' . $this->data ['id'] );
				
				$msg = array (
						'rtn_code' => 0,
						'content' => '编辑分类成功!' 
				);
				echo json_encode ( $msg );
				exit ();
			} else {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '编辑分类失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		} else {
			// 加载js文件
			$this->set ( 'scriptList', array (
					'focus.js' 
			) );
			
			$id = $this->params ['id'];
			
			$this->loadModel ( "FocusCate" );
			$CateInfo = $this->FocusCate->GetFocusCateByID ( $id );
			$this->set ( 'CateInfo', $CateInfo );
		}
	}
	/**
	 * 删除分类
	 */
	public function deleteCate() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Focus" );
		$conditions = array (
				'cate_id' => $id 
		);
		$count = $this->Focus->find ( 'count', array (
				'conditions' => $conditions 
		) );
		
		if ($count > 0) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '请先删除焦点图数据!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "FocusCate" );
		if ($this->FocusCate->del ( $id )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除焦点图分类,ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>